#!/bin/bash
#
# Copyright 2011 Google Inc. All Rights Reserved.
# Author: sdd@google.com (Salvatore Domenick Desiano)
#
# Create an additional edit clone within a GLP checkout. See glp-common.sh for
# more information on the development process used by GLP developers.

# Read parameters and initialize variables.
source "$(dirname "$0")/glp-common.sh"
declare -r name="${1}"
if [[ -z "${name}" ]]; then glp::die "usage: $0 clone_name"; fi
glp::initialize_checkout_variables "${name}"
if [[ -e "${EDIT_CLONE}" ]]; then
  glp::die "refusing to overwrite ${EDIT_CLONE}";
fi
echo "Creating edit clone in ${EDIT_CLONE}..."

echo '  cloning upstream clone into edit clone...'
hg clone --quiet "${UPSTREAM_CLONE}" "${EDIT_CLONE}" || glp::die 'failure.'

# Prevent the edit clone from pushing anywhere directly and connect pulls
# directly to the upstream repository.
echo '  updating edit clone default repository...'
declare -r temp_edit_hgrc="$(mktemp -t glp-checkout)"
grep -v '^default' "${EDIT_CLONE}/.hg/hgrc" > "${temp_edit_hgrc}"
echo "default = ${HG_URL}" >> "${temp_edit_hgrc}"
echo "default-push = ." >> "${temp_edit_hgrc}"
mv -f "${temp_edit_hgrc}" "${EDIT_CLONE}/.hg/hgrc" || glp::die 'failure.'
rm -fr "${temp_edit_hgrc}"

echo "Edit clone created in ${EDIT_CLONE}."
